<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/4/4
 * Time: 10:50
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Admin;
use think\Error;

class FleetLogic extends BaseLogic {

    private $adminModel;

    public function __construct()
    {
        parent::__construct();
        $this->adminModel = new Admin();
    }

    public function ajaxIndex($tempWhere, $nowPage){
        $where[] = ['role_id','eq',2];
        $role = session('admin_info');
        if($role['role_id']==2){
            $where[] = ['id','eq',$role['id']];
        }
        $tempWhere['username'] && $where[] = ['nick_name','like','%'.$tempWhere['username'].'%'];
        $adminCount = $this->adminModel->with('role')->where($where)->count();
        if($adminCount <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }else{
            $adminList = $this->adminModel->with('role')->hidden(['update_time', 'password', 'salt'])->page($nowPage, self::$pageNum)->where($where)->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $adminList, $adminCount);
        }
    }

    public function ajaxShop($tempWhere, $nowPage){
        $role = session('admin_info');
        $where[] = ['role_id','eq',3];
        if($role['role_id']==3){
            $where[] = ['id','eq',$role['id']];
        }
        $tempWhere['username'] && $where[] = ['nick_name','like','%'.$tempWhere['username'].'%'];
        $adminCount = $this->adminModel->with('role')->where($where)->count();
        if($adminCount <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }else{
            $adminList = $this->adminModel->with('role')->hidden(['update_time', 'password', 'salt'])->page($nowPage, self::$pageNum)->where($where)->select();

            return ajaxArr(ErrorCode::$SUCCESS, 'success', $adminList, $adminCount);
        }
    }
    //详情
    public function info($id){
        $info = Admin::get($id);
        return $info;
    }

    /**
     * 处理管理员逻辑
     * @param $data
     */
    public function handle($data){
        $act = $data['act'];
        $res = false;
        unset($data['act']);
        $validate = new \app\admin\validate\Admin();
        if ($act == 'add' || $act == 'edit'){
            if (!$validate->check($data)) {
                return ajaxArr(ErrorCode::$PARAMVALIDATE, $validate->getError());
            }
        }
        switch ($act){
            case 'add':
                $this->adminModel->nick_name = $data['nick_name'];
                $this->adminModel->name = $data['name'];
                $this->adminModel->status = $data['status'];
                $this->adminModel->role_id = 2;
                $this->adminModel->password = $data['password'];
                $this->adminModel->logo = $data['logo'];
                $this->adminModel->mobile = $data['mobile'];
                $res = $this->adminModel->save();
                break;
            case 'edit':
                $check = $this->adminModel->where('id','neq',$data['id'])->where('name','eq',$data['name'])->count();
                if($check){
                    return ajaxArr(ErrorCode::$INFONULL, '该用户已存在');
                }
                $admin = $this->adminModel->get($data['id']);
                if ($data['password']){
                    $admin->password = $data['password'];
                }
                $admin->nick_name = $data['nick_name'];
                $admin->name = $data['name'];
                $admin->is_admin = $data['is_admin'];
                $admin->status = $data['status'];
                $admin->logo = $data['logo'];
                $admin->mobile = $data['mobile'];
                $admin->update_time = time();
                $res = $admin->save();
                break;
            case 'del':
                $admin = $this->adminModel->get($data['id']);
                if (!$admin){
                    return ajaxArr(ErrorCode::$INFONULL, '该管理员不存在');
                }
                $res = $admin->delete();
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
}